
$(function() {

	$("input[id^='material_s']:visible").autocomplete(Test.material);
	
	_change("input[id='place']:visible", "select[id^='place_t']:visible");
	
	$("select[id^='category_']:visible").selectChain({
		target: $("select[id^='subclass_']:visible"),
		key: "code",
		value: "name",
		url: 'material.php?do=Amount.ajax_category',
		data: { ajax: true }
	}).trigger('change');
	
	Tip.emp('负责人');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("负责人：");
		} else if (a == 'oper') {
			label.html("录入员：");
		} 
	});
});

var Amount = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Amount.audit&item=56', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},
		
					"添加": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
		
						var form = $("#form_add")[0];
		
						var body = { };
						
						body.emp = form.emp.value;
						body.place = form.place.value;
						body.date = form.date.value;
						
						body.material = form.material.value;
						body.type = form.type.value;
		
						body.name = form.name.value;
						body.standard = _standard(form);
						body.price2 = form.price2.value;
						body.unit2 = form.unit2.value;
						body.density = form.density.value;
		
						body.unit = form.unit.value;
						body.amount = form.amount.value;
									
						body.content = form.content.value;
		
						$(this).dialog("close");
		
						$.post('material.php?do=Amount.addPost', body, function(data) {
							$(result).html(data);
							// alert("操作成功！");
						});
					}
				});
		
				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},
		
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'material.php?do=Amount.edit&id=' + id;
		var c2 = 'material.php?do=Amount.view&id=' + id;

		var c3 = 'material.php?do=Amount.status&id=' + id;

		var c6 = 'material.php?do=Out.add&id=' + id;
		var c7 = 'material.php?do=PanDian.add&id=' + id;
		var c8 = 'material.php?do=Amount.record&id=' + id;
		
		var c9 = 'material.php?do=Amount.exchange&id=' + id;
		
		var a1 = ['编辑信息', 'url', 'Amount.dialog_edit_', '650x440'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];

		// var a3 = ['暂停使用', 'url', 'Amount.list_stop'];
		// var a4 = ['停止使用', 'url', 'Amount.list_cancel'];
		// var a5 = ['恢复正常', 'url', 'Amount.list_ok'];

		var a6 = ['直接出库', 'url', 'Amount.dialog_out', '700x440', '直接出库'];
		var a7 = ['物料盘点', 'url', 'Amount.dialog_pandian', '650x440', '物料盘点'];
		// var a8 = ['领料申请', 'url', 'Amount.dialog_record', '650x420', '领料申请'];
		
		var a9 = ['单位换算', 'url', 'Amount.dialog_exchange', '700x440', '单位换算'];

		if (status == 1) {
			var s = c1 + ", " + c2 + ", " + c6 + ", " + c7 + ", " + c9;

			return popup(e, [a1, a2, a6, a7, a9], s);
		}

		var s = c1 + ", " + c2 + ", " + c9;

		return popup(e, [a1, a2, a9], s);
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Amount.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						
						body.emp = form.emp.value;
						
						body.name = form.name.value;
						body.standard = form.standard.value;
						body.unit = form.unit.value;
						body.price = form.price.value;
						body.amount = form.amount.value;
						body.place = form.place.value;
						body.content = form.content.value;
						
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'material.php?do=Amount.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	list_stop: function (url, view) {
		$.getJSON('material.php?do=Amount.audit&item=54', function(json) {
			if (json.status == 1) {
				if (confirm("物料是否暂停使用？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	list_cancel: function (url, view) {
		$.getJSON('material.php?do=Amount.audit&item=54', function(json) {
			if (json.status == 1) {
				if (confirm("物料是否停止使用？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	list_ok: function (url, view) {
		$.getJSON('material.php?do=Amount.audit&item=54', function(json) {
			if (json.status == 1) {
				if (confirm("物料是否正常使用？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	dialog_out: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Amount.audit&item=57', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"出库": function() {						
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = {};
						body.id = form.id.value;
						
						body.emp = form.emp.value;
						body.date = form.date.value;
						
						body.kc = form.kc.value;
						body.price = form.price.value;
						
						body.receive = form.receive.value;
						if (body.receive == '2') {	
							body.receiver = form.supplier.value;
							body.receiver_x = Supplier.supplier_x(form);
						} else {
							body.receiver = form.receiver.value;
						}
												
						body.amount = form.amount.value;
						
						body.mold = form.mold.value;						
						body.content = form.content.value;
						
						amount = parseFloat(body.amount);
						kc = parseFloat(body.kc);
												
						if (amount > kc) {
							alert("物料库存不足，请重新核对再出库！");
							return false;
						}
						
						/*
						price = parseFloat(body.price);
						if (price <= 0) {
							alert("单价不能小于和等于零，请重新核对再出库！");
							return false;
						}*/

						$(this).dialog("close");
						
						var url = 'material.php?do=Out.addPost';
						$.post(url, body, function(data) {
							alert("操作成功！");
							Amount.list_();
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_pandian: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Amount.audit&item=57', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = {};
						body.id = form.id.value;
						
						body.emp = form.emp.value;
						body.date = form.date.value;
						
						body.price = form.price.value;
						body.amount = form.amount.value;
						body.content = form.content.value;
												
						price = parseFloat(body.price);
						if (price <= 0) {
							alert("单价不能小于和等于零，请重新核对再盘点！");
							return false;
						}

						$(this).dialog("close");

						var url = 'material.php?do=PanDian.addPost';
						$.post(url, body, function(data) {
							alert("操作成功！");
							Amount.list_();
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_exchange: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Amount.audit&item=57', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = {};
						body.id = form.id.value;
						
						body.name = form.name.value;
						body.standard = form.standard.value;
						body.unit = form.unit.value;
						
						body.a_amount = form.a_amount.value;
						
						body.place = form.place.value;
						body.name = form.name.value;
						body.standard = form.standard.value;						
						body.unit = form.unit.value;
						
						body.amount = form.amount.value;
						body.price = form.price.value;
						
						body.content = form.content.value;
						
						price = parseFloat(body.price);
						if (price <= 0) {
							alert("单价不能小于和等于零，请重新核对！");
							return false;
						}						
						
						if (is_null(body.unit)) {
							alert("单位不能为空！");
							return false;
						}
						
						if (is_null(body.amount)) {
							alert("数量不能为空！");
							return false;
						}
						
						if (is_null(body.price)) {
							alert("价格不能为空！");
							return false;
						}
						
						if (Amount.exchange_(form)) {
							alert("原物料与新物料不能一样，请重新核对！");
							return false;
						}
						
						$(this).dialog("close");

						var url = 'material.php?do=Amount.exchangePost';
						$.post(url, body, function(data) {
							alert("操作成功！");
							Amount.list_();
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	exchange_: function (form) {
		if (form.a_name.value != form.name.value) {
			return false;
		}
		
		if (form.a_standard.value != form.standard.value) {
			return false;
		}
		
		if (form.a_unit.value != form.unit.value) {
			return false;
		}
		
		return true;
	},
	
	dialog_record: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Amount.audit&item=57', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"添加": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = {};
						body.id = form.id.value;
						body.used = form.used.value;
						body.amount = form.amount.value;
						
						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'material.php?do=Amount.recordPost';
						$.post(url, body, function(data) {
							alert("操作成功！");
							Amount.list_();
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	list_: function () {
		var result = Common.result();
		var url = 'material.php?do=Amount.findPost';

		var p2 = $.cookie('p2');
		if (p2 != null) {
			url += p2;
		}

		$.get(url, function(data) {
			$(result).html(data);
		});
	},

	cookie_cart: function () {
		var json = $.cookie('cart_used');
		if (json != null && json.length > 0) {
			return $.evalJSON(json);
		}

		return new Array();
	},

	cart_empty: function () {
		if (!confirm("确认是否清空？")) {
			return false;
		}

		$.cookie('cart_used', null);

		$("#cart").text("领料单有[0]条明细");
	},

	cart_add: function (id, amount) {
		var cart = this.cookie_cart();

		for (var i = 0; i < cart.length; i++) {
			if (cart[i][0] == id) {
				alert("此信息在领料单已存在，请不要重复添加！");
	            return false;
	        }
	    }

		var mm = [id, null, null, amount];
		cart.push(mm);

		$.cookie('cart_used', $.toJSON(cart));
		alert("已成功添加至领料单！");

		if (cart != null && cart.length > 0) {
			$("#cart").text("领料单有[" + cart.length + "]条明细");
		}
	},

	dialog_cart_my: function () {
		var json = $.cookie('cart_used');
		if (json == null) {
			alert("领料单为空，请选择您要申领的信息。");
			return false;
		}

		$('#dialog_add').dialog('option', 'buttons', {
			"提交申请": function() {
				if (!confirm("确认是否提交领料申请？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };

				body.mold = form.mold.value;
				body.name = form.name.value;
				body.content = form.content.value;

				$(this).dialog("close");			

				$.post('material.php?do=Used.addPost', body, function(data) {
					$.cookie('cart_used', null);

					$("#cart").text("领料单有[0]条明细");

					alert("操作成功！");
				});
			},

			"继续选料": function() {
				$(this).dialog("close");
			}
		});

		var url = "material.php?do=Used.add";

		dialog_add(url, '申请用料', '800x500');
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'material.php?do=Amount.delete' + p2;	
		var c9 = 'material.php?do=Amount.xls' + p2;	
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];				
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c9;

		return popup(e, [a1, a9], s);
	}
};
